package org.xenon.vote.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 防止过度申请验证码
 */
@WebFilter(filterName = "CaptchaFilter", urlPatterns = "/captcha/get")
public class CaptchaFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpSession session = ((HttpServletRequest) req).getSession();
        Object captchaTime = session.getAttribute("captchaTime");
        if(captchaTime != null&&System.currentTimeMillis() - ((Long)captchaTime) < 1000)
            return;
        chain.doFilter(req, resp);
        session.setAttribute("captchaTime", System.currentTimeMillis());
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
